import { Request, RequestHandler, Response } from 'express';
import {
  IGetResearcherByIdReq,
  IAddResearcherReq,
  IUpdateResearcherReq,
  IDeleteResearcherReq
} from '../../models/researcher.model';
import * as ResearcherService from '../services/researcher.service';

/**
 * Get active researcher records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getResearchers: RequestHandler = async (req: Request, res: Response) => {
  try {
    const researchers = await ResearcherService.getResearchers();

    res.status(200).json({
      data: researchers,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchers.controller][getResearchers][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '科研人员信息列表获取失败！'
    });
  }
};

/**
 * Get researcher record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getResearcherById: RequestHandler = async (req: IGetResearcherByIdReq, res: Response) => {
  try {
    const researcher = await ResearcherService.getResearcherById(req.params.id);

    res.status(200).json({
      data: researcher,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchers.controller][getResearcherById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '科研人员信息获取失败！'
    });
  }
};

/**
 * Inserts a new researcher record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addResearcher: RequestHandler = async (req: IAddResearcherReq, res: Response) => {
  try {
    const result = await ResearcherService.insertResearcher(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchers.controller][addResearcher][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    if (error.code === 'ER_DUP_ENTRY') {
        res.status(500).json({
            message: '科研人员职工号已存在！'
          });
    } else {
        res.status(500).json({
            message: '科研人员信息保存失败！'
         });
    }
  }
};

/**
 * Updates existing researcher record
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const updateResearcherById: RequestHandler = async (req: IUpdateResearcherReq, res: Response) => {
  try {
    const result = await ResearcherService.updateResearcher({...req.body, id: req.params.id});

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchers.controller][updateResearcherById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    if (error.code === 'ER_DUP_ENTRY') {
        res.status(500).json({
            message: '科研人员名称重复，请重新输入！'
          });
    } else {
        res.status(500).json({
            message: '科研人员信息更新失败！'
         });
    }
  }
};

/**
 * deletes a researcher
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteResearcherById: RequestHandler = async (req: IDeleteResearcherReq, res: Response) => {
  try {
    const result = await ResearcherService.deleteResearcher(req.params.id);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[researchers.controller][deleteResearcherById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '科研人员信息删除失败！'
    });
  }
};